package com.huluobo.a2307douyindemo.utils

import BaseCallBack
import android.content.Context
import android.graphics.Color
import android.icu.text.CaseMap.Title
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.huluobo.a2307douyindemo.R
import org.w3c.dom.Text

/**
 *  Created by LC on 2025/9/27.
 */
class BottomSheetDialog<T> : BottomSheetDialog {
    private lateinit var callback: BaseCallBack<T>
    private lateinit var current: TextView
    private val handler = Handler(Looper.getMainLooper())
    private lateinit var cancel: TextView
    private lateinit var layout: LinearLayout

    class Items<T>(val label: String, var value: T)

    constructor(context: Context) : super(context) {
        layout = LinearLayout(context)
        layout.layoutParams =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        layout.orientation = LinearLayout.VERTICAL
        setContentView(layout)
    }

    constructor(context: Context, title: String) : super(context) {
        layout = LinearLayout(context)
        layout.layoutParams =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        layout.orientation = LinearLayout.VERTICAL
        setContentView(layout)
        val textView = TextView(context)
        textView.text = title
        textView.layoutParams =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        textView.gravity = Gravity.CENTER
        textView.setTextColor(Color.BLACK)
        textView.setPadding(0, DensityUtils.dp2px(12f), 0, DensityUtils.dp2px(12f))
        layout.addView(textView)
        val view = View(context)
        view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(12f))
        view.setBackgroundColor(context.getColor(R.color.background))
        layout.addView(view)
    }

    constructor(context: Context, layout: LinearLayout) : super(context) {
        this.layout = layout
        setContentView(layout)
    }

    fun setItems(items: List<Items<T>>) {
        for (item in items) {
            val textView = TextView(context)
            textView.text = item.label
            textView.layoutParams =
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            textView.background = AppCompatResources.getDrawable(context, R.drawable.cell)
            textView.isClickable = true
            textView.gravity = Gravity.CENTER
            textView.setTextColor(Color.BLACK)
            textView.setPadding(0, DensityUtils.dp2px(12f), 0, DensityUtils.dp2px(12f))
            layout.addView(textView)
            textView.setOnClickListener {
                handler.post {
                    current = textView
                    current.setTextColor(Color.BLACK)
                    textView.setTextColor(context.getColor(R.color.active_color))
                }
                dismiss()
                callback.success(item.value)
            }
        }

        val view = View(context)
        view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(11f))
        view.setBackgroundColor(context.getColor(R.color.background))
        layout.addView(view)
        cancel = TextView(context)
        cancel.text = "取消"
        cancel.layoutParams =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        cancel.background = AppCompatResources.getDrawable(context, R.drawable.cell)
        cancel.isClickable = true
        cancel.gravity = Gravity.CENTER
        cancel.setTextColor(Color.BLACK)
        cancel.setPadding(0, DensityUtils.dp2px(12f), 0, DensityUtils.dp2px(20f))
        cancel.setOnClickListener { dismiss() }
        layout.addView(cancel)
    }

    //取消按钮是否可见
    fun cancelEnable(show: Boolean) {
        cancel.visibility = if (show) View.VISIBLE else View.GONE
    }

    fun setOnDismissListener(callBack: BaseCallBack<T>) {
        this.callback = callBack
    }
}